package controller;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Iterator;

import model.SpecialOrder;
import model.SpecialOrderContainer;
import controller.StorageController;

public class SpecialOrderController {

	private static SpecialOrderController specialOrderController = null;
	private static SpecialOrderContainer specialOrderContainer; 
	
	private SpecialOrderController(){
		specialOrderContainer = SpecialOrderContainer.getInstance();
	}
	
	public static SpecialOrderController getInstance(){
		if(specialOrderController == null){
			specialOrderController = new SpecialOrderController();
		}
		return specialOrderController;
	}
	
	public void addItemToSpecialOrder(int itemId, int orderId){
		SpecialOrder o = specialOrderContainer.findSpecialOrder(orderId);
		o.addItem(itemId, 1);
	}
	
	public ArrayList<String> getSpecialOrders(int id){
		return SpecialOrderContainer.getInstance().getSpecialOrders(id);
	}
	//int id, DateFormat date, boolean isPaid, String status, int OrderId	
	public String getId(int id){
	//	return specialOrderContainer.findSpecialOrder(id).getSpecialOrderId();
		return null; /// mangler
	}
	
	public void setId(int id, int newId){
		specialOrderContainer.findSpecialOrder(id).setSpecialOrderId(newId);
	}

	public DateFormat getDate(int id){
		return specialOrderContainer.findSpecialOrder(id).getDate();
	}
	
	public void setDate(int id){
		specialOrderContainer.findSpecialOrder(id).setDate(DateFormat.getDateTimeInstance());
	}

	public boolean isPaid(int id){
		return specialOrderContainer.findSpecialOrder(id).isPaid();
	}
	
	public void setPaid(int id, boolean isPaid){
		specialOrderContainer.findSpecialOrder(id).setPaid(isPaid);
	}

	public String getStatus(int id){
		return specialOrderContainer.findSpecialOrder(id).getStatus();
	}
	
	public void setStatus(int id, String status){
		specialOrderContainer.findSpecialOrder(id).setStatus(status);
	}
	
	public int getSpecialOrderId(int id){
		return specialOrderContainer.findSpecialOrder(id).getSpecialOrderId();
	}
	
	public void setSpecialOrderId(int id, int specialOrderId){
		specialOrderContainer.findSpecialOrder(id).setSpecialOrderId(specialOrderId);
	}
	public int getCustomerId(int id) {
		return specialOrderContainer.findSpecialOrder(id).getCustomerId();
	}
		
	public void setCustomerId(int id,int customerId) {
		specialOrderContainer.findSpecialOrder(id).setCustomerId(customerId);
			
	}
	
	public ArrayList<Object[]> getItems(int orderId){
		ArrayList<Object[]> al = new ArrayList<Object[]>();
		Iterator it = specialOrderContainer.findSpecialOrder(orderId).getHashMap().keySet().iterator();
		while(it.hasNext()){
			
			int itemId = (Integer)it.next();
			int itemAmount = specialOrderContainer.findSpecialOrder(orderId).getHashMap().get(itemId);
			String itemName = StorageController.getInstance().getName(String.valueOf(itemId));
			Double itemPrice = StorageController.getInstance().getPrice(String.valueOf(itemId));
			System.out.println(itemId + " " + itemName + " " + itemAmount + " " + itemPrice);
			al.add(new Object[]{ itemId, itemName, itemAmount, itemPrice});
		}
		
		for(Object[] o : al){
			
		}
		return al;
	}
	
	//** Creates an special order object */
	public void createSpecialOrder(int specialOrderId, boolean isPaid, String status, int customerId, String deliveryDate, String delivery){
		specialOrderContainer.addSpecialOrder(new SpecialOrder(specialOrderId, DateFormat.getDateTimeInstance(), isPaid, status, customerId, deliveryDate, delivery));
	}
	
	//** Updates an order object */
	public void updateSpecialOrder(int id, DateFormat date, boolean isPaid, String status, int customerId){
		SpecialOrder specialOrder = specialOrderContainer.findSpecialOrder(id);
		specialOrder.setSpecialOrderId(id);
		specialOrder.setDate(date);
		specialOrder.setPaid(isPaid);
		specialOrder.setStatus(status);
		specialOrder.setCustomerId(customerId);
	}
}
